package com.java2207.group1.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.java2207.group1.bo.CallbackBo;
import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.common.R;
import com.java2207.group1.listener.WebCallbackListener;
import com.java2207.group1.pojo.Callback;
import com.java2207.group1.service.CallbackService;
import com.java2207.group1.vo.callback.BarVo;
import com.java2207.group1.vo.callback.PieVo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 回访记录明细
 */
@RestController
@RequestMapping("/callback")
public class CallbackController {

    @Autowired
    private CallbackService callbackService;

    //(模糊)查询和分页
    @RequestMapping("/callbackList")
    public LayuiPageVo<Callback> callbackPageData(@RequestParam Map<String, Object> params) {
        return callbackService.callbackPageData(params);
    }


    //根据 id 删除回访记录
    @RequestMapping("/deleteCallback")
    public R deleteConsult(Integer id) {
        int i = callbackService.deleteCallback(id);
        if (i == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    // 添加回访记录
    @RequestMapping("addCallback")
    public R addConsult(Callback callback) {
        int i = callbackService.addCallback(callback);
        if (i == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    // 修改回访记录
    @RequestMapping("/updateCallback")
    public R updateCallback(Callback callback) {
        int i = callbackService.updateCallback(callback);
        if (i == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //批量删除回访记录
    @GetMapping("/batchCal")
    public R batchCal(String ids) {
        int i = callbackService.batchCal(ids);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    //  回访人员回访比例 柱状图
    @RequestMapping("/echartBar")
    public R echartBar() {

        List<BarVo> list = callbackService.setBarData();

        System.out.println(list);
        return R.ok().data("callback_nameBar", list);
    }

    // 咨询类型 饼图
    @RequestMapping("/echartsPie")
    public R echartsPie() {
        List<PieVo> list = callbackService.setPieData();
        return R.ok().data("typePie", list);
    }

    @Autowired
    private WebCallbackListener webCallbackListener;

    // 下载 excel 模板
    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //读取服务器上的模板文件 写到浏览器
        String realPath = request.getServletContext().getRealPath("/template/回访记录表模板.xlsx");

        File file = new File(realPath);

        byte[] bytes = FileUtils.readFileToByteArray(file);
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("回访记录表模板", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write(bytes);

        outputStream.close();

    }


    //导出列表数据     把满足条件的数据 查询出来  写到excel 中  ，把excel 写到浏览器
    @RequestMapping("/export")
    public void export(Callback callback, HttpServletResponse response) throws IOException {
        //下载excel 的 响应数据格式
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("回访记录信息导出", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, CallbackBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();
        //准备满足条件的集合数据
        List<CallbackBo> callbacks = callbackService.callbackExcelData(callback);
        // 把这个集合数据 写到 输出流
        sheet.doWrite(callbacks);
    }


    // 读取excel 文件数据 存到数据库中
    @RequestMapping("/import")
    public R importExcel(MultipartFile file) throws IOException {
        // 避免预检请求报错
        if (file != null) {
            /**
             * 参数1  文件流
             * 参数2  读取到的数据封装到哪个数据类型中
             * 参数3  监听器  每读取一行数据 就会调用监听器中的 invoke  方法
             * */
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), CallbackBo.class, webCallbackListener);
            // 得到 sheet 对象
            ExcelReaderSheetBuilder sheet = read.sheet();
            // 读取sheet
            sheet.doRead();
        }
        return R.ok();
    }


}
